﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace IRCBot
{
    public partial class ConsoleLog : Form
    {

        void ConnectIRC(string server, int port)
        {
            Log("Opening connection to IRC (" + server + ":" + port + ")");
            ircClient = new TcpClient(server, port);
            if (ircClient.Connected)
            {
                Log("Connected to IRC");
                cT = new Thread(new ThreadStart(IRCHandler));
                cT.Start();
            }
        }

        void IRCHandler()
        {
            string parsed;

            ircStream = ircClient.GetStream();
            ircOutput = new StreamWriter(ircStream);
            ircInput = new StreamReader(ircStream);

            ircOutput.WriteLine("NICK " + ircNick);
            ircOutput.WriteLine("USER miceIRC * * :miceIRC");
            ircOutput.Flush();

            while (ircClient.Connected)
            {
                parsed = ircInput.ReadLine();

                string[] raw = parsed.Split(' ');

                if (raw[0] == "PING")
                { // Answer ping
                    ircOutput.WriteLine("PONG " + raw[1]);
                    ircOutput.Flush();
                }

                if (raw[1] == "001")
                {
                    IRCInit();
                    ssstatus.Text = "Connected";
                }

                if (raw[1] == "JOIN")
                {
                    if (raw[2] == ircChannel)
                    {
                        Log(ircNick + " arrived in " + ircChannel + ", IRC connection finalized");
                    }
                }

                if (raw[1] == "PRIVMSG")
                {
                    //Manipulate the array, remove :'s and put them into strings.
                    string[] parsename = raw[0].Split('!');
                    string name = parsename[0].Substring(1);
                    string cmd = raw[3].Substring(1);
                    string dest = raw[2];

                    if (dest == ircChannel)
                    {
                        if (name == ircAdminNick)
                        {
                            if (cmd == "!join")
                            {
                                Join(raw[4]);
                                Message(dest, "Fine, I'll join that channel...");
                            }

                            if (cmd == "!connected")
                            {
                                Message(dest, "Currently " + (gCon ? "connected to" : "disconnected from") + " Glider");
                            }

                            if (cmd == "!status")
                            {
                                gCommand("/status");
                            }

                            if (cmd == "!char")
                            {
                                if (Attached)
                                {
                                    gCommand("/status");
                                    Thread.Sleep(200);
                                    int percent = ((XP / NextXP) * 100);
                                    Message(dest, Character + " the level " + Level + " " + Class);
                                    if (Level == "80")
                                        Message(dest, "09HP: " + Health + "% - 12Mana: " + Mana);
                                    else
                                        Message(dest, "09HP: " + Health + "% - 12Mana: " + Mana + " - 06XP: " + XP + "/" + NextXP + " (" + percent.ToString() + "%)");
                                    Message(dest, "Currently " + (Attached ? "attacehd to" : "detached from") + " World of Warcraft");
                                }
                                else
                                {
                                    Message(dest, "4ERROR: Not attached to World of Warcraft.");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}